home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-Native.exe / {app} / Samples / Demo7 / src / Sample_Demo7.cpp next >
Encoding:
C/C++ Source or Header  |  2005-08-21  |  18.7 KB  |  444 lines

  1. /************************************************************************
  2.     filename:   Sample_Demo6.cpp
  3.     created:    20/8/2005
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #include "Sample_Demo7.h"
  25. #include "CEGUI.h"
  26. #include "CEGuiBaseApplication.h"
  27.  
  28. #include <cstdlib>
  29.  
  30. #if defined( __WIN32__ ) || defined( _WIN32 )
  31. #define WIN32_LEAN_AND_MEAN
  32. #include "windows.h"
  33.  
  34. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
  35. #else
  36. int main(int argc, char *argv[])
  37. #endif
  38. {
  39.     // This is a basic start-up for the sample application which is
  40.     // object orientated in nature, so we just need an instance of
  41.     // the CEGuiSample based object and then tell that sample application
  42.     // to run.  All of the samples will use code similar to this in the
  43.     // main/WinMain function.
  44.     Demo7Sample app;
  45.     return app.run();
  46. }
  47.  
  48.  
  49. /*************************************************************************
  50.     Sample specific initialisation goes here.
  51. *************************************************************************/
  52. bool Demo7Sample::initialiseSample()
  53. {
  54.     using namespace CEGUI;
  55.  
  56.     // we will use of the WindowManager.
  57.     WindowManager& winMgr = WindowManager::getSingleton();
  58.  
  59.     // load scheme and set up defaults
  60.     SchemeManager::getSingleton().loadScheme("../datafiles/schemes/TaharezLook.scheme");
  61.     System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
  62.     FontManager::getSingleton().createFont("../datafiles/fonts/Commonwealth-10.font");
  63.  
  64.     // load an image to use as a background
  65.     ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "../datafiles/imagesets/GPN-2000-001437.tga");
  66.  
  67.     // here we will use a StaticImage as the root, then we can use it to place a background image
  68.     StaticImage* background = static_cast<StaticImage*>(winMgr.createWindow("TaharezLook/StaticImage", "background_wnd"));
  69.     // set position and size
  70.     background->setPosition(Point(0, 0));
  71.     background->setSize(Size(1, 1));
  72.     // disable frame and standard background
  73.     background->setFrameEnabled(false);
  74.     background->setBackgroundEnabled(false);
  75.     // set the background image
  76.     background->setImage("BackgroundImage", "full_image");
  77.     // install this as the root GUI sheet
  78.     System::getSingleton().setGUISheet(background);
  79.  
  80.     // now we create a DefaultWindow which we will attach all the widgets to.  We could
  81.     // have attached them to the background StaticImage, though we want to be a bit tricky
  82.     // since we do not wish the background to be faded by the slider - so we create this
  83.     // container window so we can affect all the other widgets, but leave the background
  84.     // unchanged.
  85.     Window* sheet = winMgr.createWindow("DefaultWindow", "root_wnd");
  86.     // attach this to the 'real' root
  87.     background->addChildWindow(sheet);
  88.  
  89.     // do demo stuff
  90.     createDemoWindows();
  91.     initDemoEventWiring();
  92.  
  93.     // success!
  94.     return true;
  95. }
  96.  
  97. /*************************************************************************
  98.     Cleans up resources allocated in the initialiseSample call.
  99. *************************************************************************/
  100. void Demo7Sample::cleanupSample()
  101. {
  102.     // nothing to do here!
  103. }
  104.  
  105. /*************************************************************************
  106.     create the windows & widgets for this demo
  107. *************************************************************************/
  108. void Demo7Sample::createDemoWindows(void)
  109. {
  110.     using namespace CEGUI;
  111.  
  112.     WindowManager& winMgr = WindowManager::getSingleton();
  113.     Window* root = winMgr.getWindow("root_wnd");
  114.  
  115.     // Create first frame window
  116.     FrameWindow* fwnd1 = static_cast<FrameWindow*>(winMgr.createWindow("TaharezLook/FrameWindow", "Demo7/Window1"));
  117.     root->addChildWindow(fwnd1);
  118.     fwnd1->setMinimumSize(Size(0.2f, 0.2f));
  119.     fwnd1->setMaximumSize(Size(0.8f, 0.8f));
  120.     fwnd1->setPosition(Point(0.2f, 0.2f));
  121.     fwnd1->setSize(Size(0.5f, 0.5f));
  122.     fwnd1->setText("Demo 7 - Window 1");
  123.     fwnd1->setCloseButtonEnabled(false);
  124.  
  125.     // create second frame window
  126.     FrameWindow* fwnd2 = static_cast<FrameWindow*>(winMgr.createWindow("TaharezLook/FrameWindow", "Demo7/Window2"));
  127.     root->addChildWindow(fwnd2);
  128.     fwnd2->setMinimumSize(Size(0.2f, 0.2f));
  129.     fwnd2->setMaximumSize(Size(0.8f, 0.8f));
  130.     fwnd2->setPosition(Point(0.4, 0.1f));
  131.     fwnd2->setSize(Size(0.5f, 0.6f));
  132.     fwnd2->setText("Demo 7 - Window 2");
  133.     fwnd2->setCloseButtonEnabled(false);
  134.  
  135.     // create third and final frame window.
  136.     FrameWindow* fwnd3 = static_cast<FrameWindow*>(winMgr.createWindow("TaharezLook/FrameWindow", "Demo7/Window3"));
  137.     root->addChildWindow(fwnd3);
  138.     fwnd3->setMinimumSize(Size(0.2f, 0.2f));
  139.     fwnd3->setMaximumSize(Size(0.8f, 0.8f));
  140.     fwnd3->setPosition(Point(0.25f, 0.4f));
  141.     fwnd3->setSize(Size(0.5f, 0.5f));
  142.     fwnd3->setText("Demo 7 - Window 3");
  143.     fwnd3->setCloseButtonEnabled(false);
  144.  
  145.     // create quit button
  146.     PushButton* btn = static_cast<PushButton*>(winMgr.createWindow("TaharezLook/Button", "Demo7/Window1/Quit"));
  147.     fwnd1->addChildWindow(btn);
  148.     btn->setMaximumSize(Size(1.0f, 1.0f));
  149.     btn->setPosition(Point(0.02f, 0.1f));
  150.     btn->setSize(Size(0.25f, 0.1f));
  151.     btn->setText("Exit Demo");
  152.  
  153.     // create first radio-button for StaticImage image selection
  154.     RadioButton* rbtn = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "Demo7/Window1/Radio1"));
  155.     fwnd1->addChildWindow(rbtn);
  156.     rbtn->setMaximumSize(Size(1.0f, 1.0f));
  157.     rbtn->setPosition(Point(0.02f, 0.23f));
  158.     rbtn->setSize(Size(0.25f, 0.075f));
  159.     rbtn->setText("Option 1");
  160.     rbtn->setID(0);
  161.  
  162.     // create second radio-button for StaticImage image selection
  163.     rbtn = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "Demo7/Window1/Radio2"));
  164.     fwnd1->addChildWindow(rbtn);
  165.     rbtn->setMaximumSize(Size(1.0f, 1.0f));
  166.     rbtn->setPosition(Point(0.02f, 0.306f));
  167.     rbtn->setSize(Size(0.25f, 0.075f));
  168.     rbtn->setText("Option 2");
  169.     rbtn->setID(1);
  170.  
  171.     // create third radio-button for StaticImage image selection
  172.     rbtn = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "Demo7/Window1/Radio3"));
  173.     fwnd1->addChildWindow(rbtn);
  174.     rbtn->setMaximumSize(Size(1.0f, 1.0f));
  175.     rbtn->setPosition(Point(0.02f, 0.382f));
  176.     rbtn->setSize(Size(0.25f, 0.075f));
  177.     rbtn->setText("Option 3");
  178.     rbtn->setID(2);
  179.  
  180.     // create checkbox to show / hide frame window 3 (with the multi-line editbox in it)
  181.     Checkbox* cbox = static_cast<Checkbox*>(winMgr.createWindow("TaharezLook/Checkbox", "Demo7/Window1/Checkbox"));
  182.     fwnd1->addChildWindow(cbox);
  183.     cbox->setMaximumSize(Size(1.0f, 1.0f));
  184.     cbox->setPosition(Point(0.02f, 0.48f));
  185.     cbox->setSize(Size(0.45f, 0.075f));
  186.     cbox->setText("Enable some option");
  187.     cbox->setSelected(true);
  188.  
  189.     // create a static text with some information in.
  190.     StaticText* stxt = static_cast<StaticText*>(winMgr.createWindow("TaharezLook/StaticText", "Demo7/Window1/Text1"));
  191.     fwnd1->addChildWindow(stxt);
  192.     stxt->setMaximumSize(Size(1.0f, 1.0f));
  193.     stxt->setPosition(Point(0.5f, 0.1f));
  194.     stxt->setSize(Size(0.45f, 0.5f));
  195.     stxt->setText("This is a re-creation of the original \"Demo7\" preview application, it shows many of the CEGUI widgets.");
  196.     stxt->setFrameEnabled(false);
  197.     stxt->setBackgroundEnabled(false);
  198.     stxt->setHorizontalFormatting(StaticText::WordWrapCentred);
  199.  
  200.     // single line edit box widget
  201.     Editbox* ebox = static_cast<Editbox*>(winMgr.createWindow("TaharezLook/Editbox", "Demo7/Window1/Editbox"));
  202.     fwnd1->addChildWindow(ebox);
  203.     ebox->setMaximumSize(Size(1.0f, 1.0f));
  204.     ebox->setPosition(Point(0.2f, 0.6f));
  205.     ebox->setSize(Size(0.6f, 0.1f));
  206.     ebox->setText("Single-line editbox");
  207.  
  208.     // large vertical 'Taharez' scrollbar
  209.     Scrollbar* sbar = static_cast<Scrollbar*>(winMgr.createWindow("TaharezLook/LargeVerticalScrollbar", "Demo7/Window1/Scrollbar1"));
  210.     fwnd1->addChildWindow(sbar);
  211.     sbar->setMinimumSize(Size(0.01f, 0.1f));
  212.     sbar->setMaximumSize(Size(0.03f, 1.0f));
  213.     sbar->setPosition(Point(0.06f, 0.6f));
  214.     sbar->setSize(Size(0.06f, 0.38f));
  215.  
  216.     // Slider to affect display alpha and progress bars
  217.     Slider* sldr = static_cast<Slider*>(winMgr.createWindow("TaharezLook/Slider", "Demo7/Window1/Slider1"));
  218.     fwnd1->addChildWindow(sldr);
  219.     sldr->setMinimumSize(Size(0.01f, 0.1f));
  220.     sldr->setMaximumSize(Size(0.03f, 1.0f));
  221.     sldr->setPosition(Point(0.9f, 0.625f));
  222.     sldr->setSize(Size(0.03f, 0.28f));
  223.     sldr->setCurrentValue(1.0f);
  224.  
  225.     // First progress bar
  226.     ProgressBar* pbar = static_cast<ProgressBar*>(winMgr.createWindow("TaharezLook/ProgressBar", "Demo7/Window2/Progbar1"));
  227.     fwnd2->addChildWindow(pbar);
  228.     pbar->setMaximumSize(Size(1.0f, 1.0f));
  229.     pbar->setPosition(Point(0.04f, 0.1f));
  230.     pbar->setSize(Size(0.94f, 0.05f));
  231.     pbar->setProgress(0.33f);
  232.  
  233.     // second progress bar in the alternative style
  234.     pbar = static_cast<ProgressBar*>(winMgr.createWindow("TaharezLook/AlternateProgressBar", "Demo7/Window2/Progbar2"));
  235.     fwnd2->addChildWindow(pbar);
  236.     pbar->setMaximumSize(Size(1.0f, 1.0f));
  237.     pbar->setPosition(Point(0.04f, 0.16f));
  238.     pbar->setSize(Size(0.94f, 0.05f));
  239.     pbar->setProgress(0.7f);
  240.  
  241.     // standard listbox
  242.     Listbox* lbox = static_cast<Listbox*>(winMgr.createWindow("TaharezLook/Listbox", "Demo7/Window2/Listbox"));
  243.     fwnd2->addChildWindow(lbox);
  244.     lbox->setMaximumSize(Size(1.0f, 1.0f));
  245.     lbox->setPosition(Point(0.04f, 0.25f));
  246.     lbox->setSize(Size(0.42f, 0.3f));
  247.  
  248.     // combobox widget
  249.     Combobox* cbobox = static_cast<Combobox*>(winMgr.createWindow("TaharezLook/Combobox", "Demo7/Window2/Combobox"));
  250.     fwnd2->addChildWindow(cbobox);
  251.     cbobox->setMaximumSize(Size(1.0f, 1.0f));
  252.     cbobox->setPosition(Point(0.5f, 0.25f));
  253.     cbobox->setSize(Size(0.42f, 0.45f));
  254.  
  255.     // multi-column list (MCL / Grid) widget
  256.     MultiColumnList* mclbox = static_cast<MultiColumnList*>(winMgr.createWindow("TaharezLook/MultiColumnList", "Demo7/Window2/MultiColumnList"));
  257.     fwnd2->addChildWindow(mclbox);
  258.     mclbox->setMaximumSize(Size(1.0f, 1.0f));
  259.     mclbox->setPosition(Point(0.05f, 0.6f));
  260.     mclbox->setSize(Size(0.9f, 0.38f));
  261.     mclbox->setSelectionMode(MultiColumnList::RowSingle);
  262.  
  263.     // StaticImage
  264.     StaticImage* simg = static_cast<StaticImage*>(winMgr.createWindow("TaharezLook/StaticImage", "Demo7/Window2/Image1"));
  265.     fwnd2->addChildWindow(simg);
  266.     simg->setMaximumSize(Size(1.0f, 1.0f));
  267.     simg->setPosition(Point(0.56f, 0.35f));
  268.     simg->setSize(Size(0.3f, 0.2f));
  269.     simg->setFrameEnabled(false);
  270.     simg->setBackgroundEnabled(false);
  271.     simg->setImage("BackgroundImage", "full_image");
  272.  
  273.     // Multi-line editbox widget
  274.     MultiLineEditbox* mleb = static_cast<MultiLineEditbox*>(winMgr.createWindow("TaharezLook/MultiLineEditbox", "Demo7/Window3/MLEditbox"));
  275.     fwnd3->addChildWindow(mleb);
  276.     mleb->setMaximumSize(Size(1.0f, 1.0f));
  277.     mleb->setPosition(Point(0.02f, 0.1f));
  278.     mleb->setSize(Size(0.96f, 0.85f));
  279.     mleb->setText("Multi-line edit box.  Edit me!");
  280.  
  281.     //
  282.     // List box setup
  283.     //
  284.     // Add items to normal listbox
  285.     lbox->addItem(new MyListItem("Listbox Item 1"));
  286.     lbox->addItem(new MyListItem("Listbox Item 2"));
  287.     lbox->addItem(new MyListItem("Listbox Item 3"));
  288.     lbox->addItem(new MyListItem("Listbox Item 4"));
  289.     lbox->addItem(new MyListItem("Listbox Item 5"));
  290.     lbox->addItem(new MyListItem("Listbox Item 6"));
  291.     lbox->addItem(new MyListItem("Listbox Item 7"));
  292.  
  293.     // add items to the combobox list
  294.     cbobox->addItem(new MyListItem("Combobox Item 1"));
  295.     cbobox->addItem(new MyListItem("Combobox Item 2"));
  296.     cbobox->addItem(new MyListItem("Combobox Item 3"));
  297.     cbobox->addItem(new MyListItem("Combobox Item 4"));
  298.     cbobox->addItem(new MyListItem("Combobox Item 5"));
  299.     cbobox->addItem(new MyListItem("Combobox Item 6"));
  300.     cbobox->addItem(new MyListItem("Combobox Item 7"));
  301.     cbobox->addItem(new MyListItem("Combobox Item 8"));
  302.     cbobox->addItem(new MyListItem("Combobox Item 9"));
  303.     cbobox->addItem(new MyListItem("Combobox Item 10"));
  304.  
  305.     // Add columns to MCL
  306.     mclbox->addColumn("Server Name", 0, 0.33f);
  307.     mclbox->addColumn("Address", 1, 0.5f);
  308.     mclbox->addColumn("Ping", 2, 0.2f);
  309.  
  310.     // Add some empty rows to the MCL
  311.     mclbox->addRow();
  312.     mclbox->addRow();
  313.     mclbox->addRow();
  314.     mclbox->addRow();
  315.     mclbox->addRow();
  316.  
  317.     // Set first row item texts for the MCL
  318.     mclbox->setItem(new MyListItem("Laggers World"), 0, 0);
  319.     mclbox->setItem(new MyListItem("yourgame.some-server.com"), 1, 0);
  320.     mclbox->setItem(new MyListItem("1000ms"), 2, 0);
  321.  
  322.     // Set second row item texts for the MCL
  323.     mclbox->setItem(new MyListItem("Super-Server"), 0, 1);
  324.     mclbox->setItem(new MyListItem("whizzy.fakenames.net"), 1, 1);
  325.     mclbox->setItem(new MyListItem("8ms"), 2, 1);
  326.  
  327.     // Set third row item texts for the MCL
  328.     mclbox->setItem(new MyListItem("Cray-Z-Eds"), 0, 2);
  329.     mclbox->setItem(new MyListItem("crayzeds.notarealserver.co.uk"), 1, 2);
  330.     mclbox->setItem(new MyListItem("43ms"), 2, 2);
  331.  
  332.     // Set fourth row item texts for the MCL
  333.     mclbox->setItem(new MyListItem("Fake IPs"), 0, 3);
  334.     mclbox->setItem(new MyListItem("123.320.42.242"), 1, 3);
  335.     mclbox->setItem(new MyListItem("63ms"), 2, 3);
  336.  
  337.     // Set fifth row item texts for the MCL
  338.     mclbox->setItem(new MyListItem("Yet Another Game Server"), 0, 4);
  339.     mclbox->setItem(new MyListItem("abc.abcdefghijklmn.org"), 1, 4);
  340.     mclbox->setItem(new MyListItem("284ms"), 2, 4);
  341. }
  342.  
  343.  
  344. /*************************************************************************
  345.     Perform required event hook-ups for this demo.
  346. *************************************************************************/
  347. void Demo7Sample::initDemoEventWiring(void)
  348. {
  349.     using namespace CEGUI;
  350.  
  351.     // Subscribe handler that quits the application
  352.     WindowManager::getSingleton().getWindow("Demo7/Window1/Quit")->
  353.         subscribeEvent(PushButton::EventClicked, Event::Subscriber(&Demo7Sample::handleQuit, this));
  354.  
  355.     // Subscribe handler that processes changes to the slider position.
  356.     WindowManager::getSingleton().getWindow("Demo7/Window1/Slider1")->
  357.         subscribeEvent(Slider::EventValueChanged, Event::Subscriber(&Demo7Sample::handleSlider, this));
  358.  
  359.     // Subscribe handler that processes changes to the checkbox selection state.
  360.     WindowManager::getSingleton().getWindow("Demo7/Window1/Checkbox")->
  361.         subscribeEvent(Checkbox::EventCheckStateChanged, Event::Subscriber(&Demo7Sample::handleCheck, this));
  362.  
  363.     // Subscribe handler that processes changes to the radio button selection state.
  364.     WindowManager::getSingleton().getWindow("Demo7/Window1/Radio1")->
  365.         subscribeEvent(RadioButton::EventSelectStateChanged, Event::Subscriber(&Demo7Sample::handleRadio, this));
  366.  
  367.     // Subscribe handler that processes changes to the radio button selection state.
  368.     WindowManager::getSingleton().getWindow("Demo7/Window1/Radio2")->
  369.         subscribeEvent(RadioButton::EventSelectStateChanged, Event::Subscriber(&Demo7Sample::handleRadio, this));
  370.  
  371.     // Subscribe handler that processes changes to the radio button selection state.
  372.     WindowManager::getSingleton().getWindow("Demo7/Window1/Radio3")->
  373.         subscribeEvent(RadioButton::EventSelectStateChanged, Event::Subscriber(&Demo7Sample::handleRadio, this));
  374. }
  375.  
  376. bool Demo7Sample::handleQuit(const CEGUI::EventArgs& e)
  377. {
  378.     // signal quit
  379.     d_sampleApp->setQuitting();
  380.  
  381.     // event was handled
  382.     return true;
  383. }
  384.  
  385. bool Demo7Sample::handleSlider(const CEGUI::EventArgs& e)
  386. {
  387.     using namespace CEGUI;
  388.  
  389.     // get the current slider value
  390.     float val = static_cast<Slider*>(static_cast<const WindowEventArgs&>(e).window)->getCurrentValue();
  391.  
  392.     // set the progress for the first bar according to the slider value
  393.     static_cast<ProgressBar*>(WindowManager::getSingleton().getWindow("Demo7/Window2/Progbar1"))->setProgress(val);
  394.     // set second bar's progress - this time the reverse of the first one
  395.     static_cast<ProgressBar*>(WindowManager::getSingleton().getWindow("Demo7/Window2/Progbar2"))->setProgress(1.0f - val);
  396.     // set the alpha on the window containing all the controls.
  397.     WindowManager::getSingleton().getWindow("root_wnd")->setAlpha(val);
  398.  
  399.     // event was handled.
  400.     return true;
  401. }
  402.  
  403. bool Demo7Sample::handleRadio(const CEGUI::EventArgs& e)
  404. {
  405.     using namespace CEGUI;
  406.  
  407.     // get the ID of the selected radio button
  408.     CEGUI::uint id = static_cast<RadioButton*>(static_cast<const WindowEventArgs&>(e).window)->getSelectedButtonInGroup()->getID();
  409.     // get the StaticImage window
  410.     StaticImage* img = static_cast<StaticImage*>(WindowManager::getSingleton().getWindow("Demo7/Window2/Image1"));
  411.  
  412.     // set an image into the StaticImage according to the ID of the selected radio button.
  413.     switch (id)
  414.     {
  415.     case 0:
  416.         img->setImage("BackgroundImage", "full_image");
  417.         break;
  418.  
  419.     case 1:
  420.         img->setImage("TaharezLook", "MouseArrow");
  421.         break;
  422.  
  423.     default:
  424.         img->setImage(0);
  425.         break;
  426.     }
  427.  
  428.     // event was handled
  429.     return true;
  430. }
  431.  
  432. bool Demo7Sample::handleCheck(const CEGUI::EventArgs& e)
  433. {
  434.     using namespace CEGUI;
  435.  
  436.     // show or hide the FrameWindow containing the multi-line editbox according to the state of the
  437.     // checkbox widget
  438.     WindowManager::getSingleton().getWindow("Demo7/Window3")->
  439.         setVisible(static_cast<Checkbox*>(static_cast<const WindowEventArgs&>(e).window)->isSelected());
  440.  
  441.     // event was handled.
  442.     return true;
  443. }
  444.